package com.pds.web.company.controller;

import com.pds.common.constant.HttpStatus;
import com.pds.common.core.controller.BaseController;
import com.pds.common.core.domain.R;
import com.pds.common.core.domain.entity.SysUser;
import com.pds.common.core.page.TableDataInfo;
import com.pds.common.utils.SecurityUtils;
import com.pds.web.company.domain.*;
import org.springframework.beans.BeanUtils;
import com.pds.web.company.service.PdsCompanyStationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 企业站房管理
 */
@RestController
@RequestMapping("/companyStation")
public class PdsCompanyStationController extends BaseController {

    @Autowired
    PdsCompanyStationService stationService;

    /**
     * 添加企业站房，无id新增，有id修改
     * @return
     */
    @PostMapping("add")
    public R add(@RequestBody PdsCompanyStationVO vo){
        PdsCompanyStationOps ops = new PdsCompanyStationOps();
        PdsCompanyStationInfo info = new PdsCompanyStationInfo();
        BeanUtils.copyProperties(vo,info);
        BeanUtils.copyProperties(vo,ops);
        info = stationService.add(info);
        ops.setStationId(info.getId());
        stationService.addOpsInfo(ops);
        return R.ok();
    }

    /**
     * 删除站房
     * @param id 站房id
     * @return
     */
    @GetMapping("delete")
    public R deleteStation(@RequestParam("id")Long id){
        stationService.deleteStation(id);
        return R.ok();
    }


    /**
     * 查询企业站房列表
     * @param params 搜索条件
     *            - companyName 企业名称/MN号/站房名称
     *            - supervisionLevel 监管级别:1重点、2非重点
     *            - area 区域id
     *            - emissionType 排放类型:1一般废水、2污水处理、3一般废气、4VOCs废气、5垃圾焚烧
     *            - industry 所属行业
     *            - opsOrganizationId 运维机构
      */
    @GetMapping(value = "/list")
    public TableDataInfo list(@RequestParam Map params){
        startPage();
        Map map = stationService.getCompanyStationList(params);
        TableDataInfo info = new TableDataInfo();
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("查询成功");
        rspData.setRows((List<?>) map.get("rows"));
        rspData.setTotal((Long) map.get("total"));
        return rspData;
    }

    /**
     * 添加企业站房运维信息
     */
    @PostMapping("addOpsInfo")
    public R addOpsInfo(@RequestBody PdsCompanyStationOps opsInfo){
        stationService.addOpsInfo(opsInfo);
        return R.ok();
    }

    /**
     * 添加企业站房运维仪器信息
     */
    @PostMapping("addDevice")
    public R addDevice(@RequestBody PdsCompanyStationOpsDevice device){
        stationService.addDevice(device);
        return R.ok();
    }

    /**
     * 删除站房运维仪器
     * @param id 仪器id
     */
    @GetMapping("deleteDevice")
    public R deleteDevice(@RequestParam("id") Long id) {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        PdsCompanyStationOpsDevice device = new PdsCompanyStationOpsDevice();
        device.setId(id);
        device.setStatus("1");
        stationService.addDevice(device);
        return R.ok();
    }

    /**
     * 查询站房详细
     * @param stationId 站房id
     * @return
     */
    @GetMapping("getDetail")
    public R getDetail(@RequestParam("stationId") Long stationId) {
        Map map = stationService.getDetail(stationId);
        return R.ok(map);
    }

    /**
     * 查询仪器详情
     * @param id 仪器id
     * @return
     */
    @GetMapping("getDeviceDetail")
    public R getDeviceDetail(@RequestParam("id") Long id) {
        PdsCompanyStationOpsDevice device = stationService.getDeviceDetail(id);
        return R.ok(device);
    }

    /**
     * 查询站房运维表单列表
     * @return
     */
    @GetMapping("getOpsFormList")
    public TableDataInfo getOpsFormList(@RequestParam Map map){
        startPage();
        List<Map> list = stationService.getOpsFormList(map);
        return getDataTable(list);
    }

    /**
     * 站房运维统计
     * @param stationId 站房id
     * @param type 类型：month本月、year本年，其他为全部
     */
    @GetMapping("getStationOpsCount")
    public R getStationOpsCount(@RequestParam("stationId") Long stationId,
                                @RequestParam(name="type",required = false) String type){
        List<Map> list = stationService.getStationOpsCount(stationId,type);
        return R.ok(list);
    }

    /**
     * 环保局给运维表单评分
     * @return
     */
    @PostMapping("epbScore")
    public R epbScore(@RequestBody PdsCompanyStationOpsForm form){
        stationService.epbScore(form.getId(),form.getEpbScore());
        return R.ok();
    }

    /**
     * 查询指定站房的运维机构信息
     * @param stationId 站房id
     */
    @GetMapping("getStationOpsOrganization")
    public R getStationOpsOrganization(@RequestParam("stationId") Long stationId){
        List<Map> list = stationService.getStationOpsOrganization(stationId);
        return R.ok(list);
    }

    /**
     * 查询指定站房的运维工程师信息
     * @param stationId 站房id
     * @param organizationId 运维机构id（可选）
     */
    @GetMapping("getStationOpsEngineer")
    public R getStationOpsEngineer(@RequestParam("stationId") Long stationId,
                                   @RequestParam(name="organizationId",required = false) Long organizationId){
        List<Map> list = stationService.getStationOpsEngineer(stationId,organizationId);
        return R.ok(list);
    }

}
